package com.wanmait.CarTrading.DAO.impl;

import com.wanmait.CarTrading.DAO.CommonDAO;
import com.wanmait.CarTrading.DAO.UserinfoDAO;
import com.wanmait.CarTrading.Util.DBHelper;
import com.wanmait.CarTrading.Util.Pager;
import com.wanmait.CarTrading.vo.Userinfo;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
@Repository("userinfoDAO")
public class UserinfoDAOImpl implements UserinfoDAO {

    public Userinfo passwordUsernameGetUserinfo(Userinfo userinfo){
        Userinfo uf=null;
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        String sql=" Select * from userinfo where username=? and password=? ";
        try {
            conn= DBHelper.getDBhelper().getConn();
            ps=conn.prepareStatement(sql);
            ps.setString(1,userinfo.getUsername());
            ps.setString(2,userinfo.getPassword());
            rs=ps.executeQuery();

            if(rs.next()) {
                uf = new Userinfo();
                uf.setPassword(rs.getString("password"));
                uf.setUsername(rs.getString("username"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return uf;
    }

    @Override
    public List<Userinfo> findAll() {
        return null;
    }

    @Override
    public Userinfo findById(Integer id) {
        return null;
    }

    @Override
    public List<Userinfo> findAll(Pager pager) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public void insert(Userinfo object) {

    }

    @Override
    public void update(Userinfo object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }
    public Userinfo findByUsername(String username) {
        Userinfo uf=null;
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        String sql=" Select * from userinfo where username=?  ";
        try {
            conn= DBHelper.getDBhelper().getConn();
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);

            rs=ps.executeQuery();

            if(rs.next()) {
                uf = new Userinfo();
                uf.setEnable(rs.getBoolean("enable"));
                uf.setUsername(rs.getString("username"));
                uf.setPassword(rs.getString("password"));
                uf.setSalt(rs.getString("salt"));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return uf;
    }
}


